package com.youlai.boot.system.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.boot.common.enums.LogModuleEnum;
import com.youlai.boot.common.annotation.RepeatSubmit;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.system.model.form.BannerForm;
import com.youlai.boot.system.model.query.BannerPageQuery;
import com.youlai.boot.common.annotation.Log;
import com.youlai.boot.system.model.vo.BannerPageVO;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * banner控制器
 * @author haoxr
 * @since 2020/11/6
 */
@Tag(name = "09.banner接口")
@RestController
@RequestMapping("/api/v1/banner")
@RequiredArgsConstructor
public class BannerController {
    private final com.youlai.boot.system.service.BannerService BannerService;
    @Operation(summary = "Banner分页列表")
    @GetMapping("/page")
    @Log(value = "文章分页列表", module = LogModuleEnum.USER)
    public PageResult<BannerPageVO> listPagedBanners(
            BannerPageQuery queryParams
    ) {
        IPage<BannerPageVO> result = BannerService.listPagedBanners(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "Banner分页列表")
    @GetMapping("/page2")
    @Log(value = "文章分页列表", module = LogModuleEnum.USER)
    public PageResult<BannerPageVO> listPaged2Banners(
            BannerPageQuery queryParams
    ) {
        IPage<BannerPageVO> result = BannerService.listPaged2Banners(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增banner")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:banner:add')")
    @RepeatSubmit
    public Result<?> saveBanner(
            @RequestBody @Valid BannerForm bannerForm
    ) {
        boolean result = BannerService.saveBanner(bannerForm);
        return Result.judge(result);
    }

    @Operation(summary = "获取banner表单数据")
    @GetMapping("/{bannerId}/form")
    public Result<BannerForm> getArticleForm(
            @Parameter(description ="文章ID") @PathVariable Long bannerId
    ) {
        BannerForm BannerForm = BannerService.getBannerForm(bannerId);
        return Result.success(BannerForm);
    }

    @Operation(summary = "修改banner")
    @PutMapping(value = "/{bannerId}")
    @PreAuthorize("@ss.hasPerm('sys:banner:edit')")
    public Result<?> updateBanner(
            @Parameter(description = "bannerId") @PathVariable Long bannerId,
            @RequestBody @Validated BannerForm bannerForm) {
        boolean result = BannerService.updateBanner(bannerId, bannerForm);
        return Result.judge(result);
    }


    @Operation(summary = "删除banner")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteBanners(
            @Parameter(description = "bannerID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = BannerService.deleteBanners(ids);
        return Result.judge(result);
    }

}